package proxies.classes.url
{
	import core.events.PutIoErrorEvent;
	import core.events.PutIoResultEvent;
	
	import proxies.Proxy;
	import proxies.classes.subscription.events.SubscriptionProxyError;
	import proxies.classes.subscription.events.SubscriptionProxyListResponse;
	import proxies.classes.url.events.URLProxyAnalyzeResponse;
	import proxies.classes.url.events.URLProxyError;
	import proxies.classes.url.events.URLProxyExtracturlsResponse;
	
	public class URLProxy extends Proxy implements IURLProxy
	{
		public static const NAME:String = "urls";
		
		public static const METHOD_ANALYZE:String = 'analyze';
		public static const METHOD_EXTRACTURLS:String = 'extracturls';
		
		public function URLProxy()
		{
			super(NAME);
		}
		
		public function analyze(urls:String):void
		{
			var parameters:Object = new Object();
			parameters.urls = urls;			
			
			sendRequest(METHOD_ANALYZE, onAnalyzeResult, onError, parameters);
		}
		
		public function extracturls(txt:String):void
		{
			var parameters:Object = new Object();
			parameters.txt = txt;			
			
			sendRequest(METHOD_EXTRACTURLS, onExtracturlsResult, onError, parameters);
		}
		
		//-------------------------Action Listeners --------------------------
		private function onAnalyzeResult(result:PutIoResultEvent):void
		{			
			// Fetch Data into an Arraylist.
			//var itemsList:Array = buildUrlList(result.results);
			var urlList:Array = new Array();
			
			var urlProxyAnalyzeResponse:URLProxyAnalyzeResponse = new URLProxyAnalyzeResponse(urlList, result.bubbles, result.cancelable);			
			dispatchEvent(urlProxyAnalyzeResponse);
		}
		
		private function onExtracturlsResult(result:PutIoResultEvent):void
		{
			// Fetch Data into an Arraylist.
			//var itemsList:Array = buildUrlList(result.results);
			var urlList:Array = new Array();
			
			var urlProxyExtracturlsResponse:URLProxyExtracturlsResponse = new URLProxyExtracturlsResponse(urlList, result.bubbles, result.cancelable);
			dispatchEvent(urlProxyExtracturlsResponse);			
		}	
		
		private function onError(error:PutIoErrorEvent):void
		{
			//Add error message
			var urlProxyError:URLProxyError = new URLProxyError(error.errorMessage, error.bubbles, error.cancelable);
			
			//Fire event
			dispatchEvent(urlProxyError);
		}
		
		private function buildUrlList(result:Object):void
		{
			
		}
	}
}